#include "stdafx.h"
#include "jeffect.h"

#include "directui.h"

#include <gtl/script/jscript.h>
#include <gtl/directui/visitor.h>
#include <gtl/directui/animats.h>

namespace gtl
{

namespace dui
{

jeffect::jeffect(const std::vector<gtl::tstr>& args)
{
	printf("construct jeffect\n");
}

inline void jeffect::run_effect(effect_func func, const v8::Arguments& args)
{
	if(m_directui == NULL)
		return;

	std::vector<gtl::tstr> vec_args;
	for(int i = 0; i < args.Length(); ++i)
	{
		vec_args.push_back(gtl::tstr(gtl::jscript::cstr(v8::String::Utf8Value(args[i]))));
	}

	////(m_effect.*func)(m_directui, vec_args);
}

template<class Animat>
void jeffect::start_animat(const v8::Arguments& args)
{
	if(m_directui == NULL)
		return;

	std::vector<gtl::tstr> vec_args;
	for(int i = 0; i < args.Length(); ++i)
	{
		vec_args.push_back(gtl::tstr(gtl::jscript::cstr(v8::String::Utf8Value(args[i]))));
	}

	animat* anmt = new Animat;
	m_animats.push_back(anmt);
	anmt->set_args(m_directui, vec_args);
	anmt->set_callback(std::bind(std::mem_fn(&animat_stopped), this, std::placeholders::_1));
	anmt->start();
}

void jeffect::animat_stopped(animat* anmt)
{
	if(anmt == NULL)
		return;

	m_animats.remove(anmt);
	delete anmt;
}

v8::Handle<v8::Value> jeffect::fade(const v8::Arguments& args)
{
	//run_effect(&effect::fade, args);
	return v8::Undefined();
}

v8::Handle<v8::Value> jeffect::roll(const v8::Arguments& args)
{
	//run_effect(&effect::roll, args);
	return v8::Undefined();
}

v8::Handle<v8::Value> jeffect::shutter(const v8::Arguments& args)
{
	//run_effect(&effect::shutter, args);
	return v8::Undefined();
}

v8::Handle<v8::Value> jeffect::move(const v8::Arguments& args)
{
	//run_effect(&effect::move, args);
	return v8::Undefined();
}

v8::Handle<v8::Value> jeffect::scale(const v8::Arguments& args)
{
	//run_effect(&effect::scale, args);
	return v8::Undefined();
}

v8::Handle<v8::Value> jeffect::stretch(const v8::Arguments& args)
{
	//run_effect(&effect::stretch, args);
	return v8::Undefined();
}

v8::Handle<v8::Value> jeffect::extrusion(const v8::Arguments& args)
{
	//run_effect(&effect::extrusion, args);
	return v8::Undefined();
}

v8::Handle<v8::Value> jeffect::flip(const v8::Arguments& args)
{
	//run_effect(&effect::flip, args);
	return v8::Undefined();
}

} // end of namespace dui

} // end of namespace gtl
